<template>
	<view class='content'>
		
		<view class="form">
			<u--form
					labelPosition="left"
					:model="model1"
					:rules="rules"
					labelWidth='80px'
					ref="uForm"
			>
				<u-form-item
						label="人脸照片"
						prop="img"
						
						ref="item1"
				>
					<u-upload
						:fileList="fileList1"
						@afterRead="afterRead"
						@delete="deletePic"
						name="1"
						:maxCount="1"
					></u-upload>
				</u-form-item>
			</u--form>
		</view>
		<view class="btn">
			<u-button type="primary" :text="model1.img == ''?'添加人脸照片':'修改人脸照片'" @click='submit'></u-button>
		</view>
		
	</view>
</template>

<script>
	import { validatePhone } from '@/utils/time.js'
	export default {
		data() {
			return {
				model1: {
					name: '',
					userPhone: '',
					img: ''
				},
				fileList1: [],
				rules: {
					'img': {
						type: 'string',
						required: true,
						message: '请上传图片',
						trigger: ['blur', 'change']
					}
				},
			}
		},
		onLoad() {
			this.getface()
		},
		methods: {
			getface(){
				var _this = this
					this.fq_post('wxsite/User/getUserFaceImg', {
						
					}, (res) => {
						console.log(res)
						if(res.code == 1){
							_this.model1.img = res.data.face_img
							if(res.data.face_img != ''){
								_this.fileList1 = [{
									url: res.data.face_img
								}]
							}
							
						}
					})
					// uni.request({
					// 	url: 'http://www.yndzfl.cn/wxsite/User/getUserFaceImg',
					// 	method: 'POST',
					// 	header:{
					// 		"Content-Type" : "application/x-www-form-urlencoded"
					// 	},
					// 	data: {
					// 		openid: uni.getStorageSync('OPENID'),
							
					// 	},
					// 	success(res) {
					// 		console.log(res)
					// 		if(res.data.code == 1){
					// 			_this.model1.img = res.data.data.face_img
					// 			_this.fileList1 = [{
					// 				url: res.data.data.face_img
					// 			}]
					// 		}
							
					// 	}
					// })
				},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					this.model1.img = result
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://www.yndzfl.cn/wxsite/User/addImgv2', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							openid: uni.getStorageSync('OPENID')
						},
						success: (res) => {
							console.log(JSON.parse(res.data))
							var data = JSON.parse(res.data)
							
							if(data.code == 1){
								setTimeout(() => {
									resolve(data.data)
								}, 1000)
							}else{
								uni.showToast({
									title: data.msg,
									icon: 'none'
								})
							}
							
						}
					});
				})
			},
			submit(){
				var _this = this
				this.$refs.uForm.validate().then(res => {
					_this.submitSuccess()
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			},
			submitSuccess(){
				var _this = this
				this.fq_post('wxsite/User/addUserFaceImgv2', {
					file: _this.model1.img
				}, (res) => {
					console.log(res)
					if(res.code == 1){
						setTimeout( ()=>{
						  // uni.navigateBack()
						},1500)
					}
				})
				// uni.request({
				// 	url: 'http://www.yndzfl.cn/wxsite/User/addUserFaceImgv2',
				// 	method: 'POST',
				// 	header:{
				// 		"Content-Type" : "application/x-www-form-urlencoded"
				// 	},
				// 	data: {
				// 		openid: uni.getStorageSync('OPENID'),
				// 		file: _this.model1.img
				// 	},
				// 	success(res) {
				// 		console.log(res)
				// 		if(res.data.code == 1){
				// 			setTimeout( ()=>{
				// 			  // uni.navigateBack()
				// 			},1500)
				// 		}
						
				// 	}
				// })
			}
		}
	}
</script>

<style scoped>
.content{
	min-height: 100vh;
	background-color: #F6F6F6;
}
.form{
	background-color: #fff;
	padding: 15px;
	
}
.btn{
	padding: 15px;
}
</style>
